class ProductModel {
  final int id;
  final String name;
  final String description;
  final double price;
  final int stock;
  final int categoryId;
  final String brand;
  final String sku;
  final String status;
  final String? createdAt;
  final String? updatedAt;
  final String imageUrl;

  ProductModel({
    required this.id,
    required this.name,
    required this.description,
    required this.price,
    required this.stock,
    required this.categoryId,
    required this.brand,
    required this.sku,
    required this.status,
    this.createdAt,
    this.updatedAt,
    required this.imageUrl,
  });

  factory ProductModel.fromJson(Map<String, dynamic> json) {
    return ProductModel(
      id: json['id'],
      name: json['name'],
      description: json['description'],
      price: json['price'] != null ? (json['price'] as num).toDouble() : 0.0,
      stock: json['stock'],
      categoryId: json['categoryId'],
      brand: json['brand'],
      sku: json['sku'],
      status: json['status'],
      createdAt: json['createdAt'],
      updatedAt: json['updatedAt'],
      imageUrl: json['imageUrl'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'price': price,
      'stock': stock,
      'categoryId': categoryId,
      'brand': brand,
      'sku': sku,
      'status': status,
      'createdAt': createdAt,
      'updatedAt': updatedAt,
      'imageUrl': imageUrl,
    };
  }

  // 判断是否为可用状态
  bool get isActive => status == 'active' && stock > 0;

  // 格式化价格显示
  String get formattedPrice => '\$${price.toStringAsFixed(2)}';
}
